<script>
  export default {
    name: 'PasswordLogin',
    options: {
      // isolated 互不影响 (默认值)
      // apply-shared 页面影响组件
      //shared 互相影响
      // 设置样式隔离，共享样式
      styleIsolation: 'shared',
    },
  }
</script>
<!-- pages/login/components/passoword.vue -->
<script setup>
  import { ref } from 'vue'
  import { passwordLoginApi } from '@/services/user.js'
  import { useRefreshTokenStore } from '@/stores/refreshToken.js'
  import { useUserStore } from '@/stores/user.js'
  const userStore = useUserStore()
  const RefreshTokenStore = useRefreshTokenStore()
  const formRef = ref(null)
  const form = ref({
    data: {
      mobile: '13230000077',
      password: 'abc12345',
    },
    rules: {
      mobile: {
        rules: [
          { required: true, errorMessage: '请填写手机号码' },
          { pattern: /^1[3-9]\d{9}$/, errorMessage: '手机号码格式不正确' },
        ],
      },
      password: {
        rules: [
          { required: true, errorMessage: '请填写密码' },
          { pattern: /[A-Za-z1-9]{8}$/, errorMessage: '密码是8位' },
        ],
      },
    },
  })
  const isAgree = ref(false)
  const login = async () => {
    await formRef.value.validate()
    if (!isAgree.value) return uni.utils.toast('请阅读并同意用户协议')
    const res = await passwordLoginApi(form.value.data)
    console.log(res)
    userStore.setBasicInfo = res.data
    RefreshTokenStore.refreshToken = res.data.refreshToken
    uni.utils.toast('登录成功')
    // uni.switchTab({url:'/pages/index/index'})
    uni[userStore.returnType]({
      url: userStore.returnUrl,
    })
  }
</script>
<template>
  <uni-forms
    class="login-form"
    ref="formRef"
    :model="form.data"
    :rules="form.rules"
  >
    <uni-forms-item name="mobile">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
        v-model="form.data.mobile"
      />
    </uni-forms-item>
    <uni-forms-item name="password">
      <uni-easyinput
        type="password"
        placeholder="请输入密码"
        :input-border="false"
        placeholder-style="color: #C3C3C5"
        v-model="form.data.password"
      />
    </uni-forms-item>
    <view class="agreement">
      <radio :checked="isAgree" color="#16C2A3" @click="isAgree = !isAgree" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="login">登 录</button>
    <navigator hover-class="none" class="uni-navigator" url=" ">
      忘记密码？
    </navigator>
  </uni-forms>
</template>

<style lang="scss">
  @import './styles.scss';
</style>
